
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>4.1 创建一个简单的单元测试 · 《CMake Cookbook》中文版</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="4.2-chinese.html" />
    
    
    <link rel="prev" href="4.0-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../preface/preface-chinese.html">
            
                <a href="../preface/preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter0/0.0-chinese.html">
            
                <a href="../chapter0/0.0-chinese.html">
            
                    
                    第0章 配置环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter0/0.1-chinese.html">
            
                <a href="../chapter0/0.1-chinese.html">
            
                    
                    0.1 获取代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter0/0.2-chinese.html">
            
                <a href="../chapter0/0.2-chinese.html">
            
                    
                    0.2 Docker镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter0/0.3-chinese.html">
            
                <a href="../chapter0/0.3-chinese.html">
            
                    
                    0.3 安装必要的软件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter0/0.4-chinese.html">
            
                <a href="../chapter0/0.4-chinese.html">
            
                    
                    0.4 测试环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter0/0.5-chinese.html">
            
                <a href="../chapter0/0.5-chinese.html">
            
                    
                    0.5 上报问题并提出改进建议
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 从可执行文件到库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 将单个源文件编译为可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 切换生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 构建和链接静态库和动态库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 用条件句控制编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 向用户显示选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter1/1.6-chinese.html">
            
                <a href="../chapter1/1.6-chinese.html">
            
                    
                    1.6 指定编译器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter1/1.7-chinese.html">
            
                <a href="../chapter1/1.7-chinese.html">
            
                    
                    1.7 切换构建类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter1/1.8-chinese.html">
            
                <a href="../chapter1/1.8-chinese.html">
            
                    
                    1.8 设置编译器选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter1/1.9-chinese.html">
            
                <a href="../chapter1/1.9-chinese.html">
            
                    
                    1.9 为语言设定标准
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter1/1.10-chinese.html">
            
                <a href="../chapter1/1.10-chinese.html">
            
                    
                    1.10 使用控制流
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter2/2.0-chinese.html">
            
                <a href="../chapter2/2.0-chinese.html">
            
                    
                    第2章 检测环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter2/2.1-chinese.html">
            
                <a href="../chapter2/2.1-chinese.html">
            
                    
                    2.1 检测操作系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter2/2.2-chinese.html">
            
                <a href="../chapter2/2.2-chinese.html">
            
                    
                    2.2 处理与平台相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter2/2.3-chinese.html">
            
                <a href="../chapter2/2.3-chinese.html">
            
                    
                    2.3 处理与编译器相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter2/2.4-chinese.html">
            
                <a href="../chapter2/2.4-chinese.html">
            
                    
                    2.4 检测处理器体系结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter2/2.5-chinese.html">
            
                <a href="../chapter2/2.5-chinese.html">
            
                    
                    2.5 检测处理器指令集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter2/2.6-chinese.html">
            
                <a href="../chapter2/2.6-chinese.html">
            
                    
                    2.6 为Eigen库使能向量化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter3/3.0-chinese.html">
            
                <a href="../chapter3/3.0-chinese.html">
            
                    
                    第3章 检测外部库和程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter3/3.1-chinese.html">
            
                <a href="../chapter3/3.1-chinese.html">
            
                    
                    3.1 检测Python解释器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter3/3.2-chinese.html">
            
                <a href="../chapter3/3.2-chinese.html">
            
                    
                    3.2 检测Python库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter3/3.3-chinese.html">
            
                <a href="../chapter3/3.3-chinese.html">
            
                    
                    3.3 检测Python模块和包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter3/3.4-chinese.html">
            
                <a href="../chapter3/3.4-chinese.html">
            
                    
                    3.4 检测BLAS和LAPACK数学库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter3/3.5-chinese.html">
            
                <a href="../chapter3/3.5-chinese.html">
            
                    
                    3.5 检测OpenMP的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter3/3.6-chinese.html">
            
                <a href="../chapter3/3.6-chinese.html">
            
                    
                    3.6 检测MPI的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter3/3.7-chinese.html">
            
                <a href="../chapter3/3.7-chinese.html">
            
                    
                    3.7 检测Eigen库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter3/3.8-chinese.html">
            
                <a href="../chapter3/3.8-chinese.html">
            
                    
                    3.8 检测Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../chapter3/3.9-chinese.html">
            
                <a href="../chapter3/3.9-chinese.html">
            
                    
                    3.9 检测外部库:Ⅰ. 使用pkg-config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../chapter3/3.10-chinese.html">
            
                <a href="../chapter3/3.10-chinese.html">
            
                    
                    3.10 检测外部库:Ⅱ. 自定义find模块
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="4.0-chinese.html">
            
                <a href="4.0-chinese.html">
            
                    
                    第4章 创建和运行测试
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.7.1" data-path="4.1-chinese.html">
            
                <a href="4.1-chinese.html">
            
                    
                    4.1 创建一个简单的单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="4.2-chinese.html">
            
                <a href="4.2-chinese.html">
            
                    
                    4.2 使用Catch2库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="4.3-chinese.html">
            
                <a href="4.3-chinese.html">
            
                    
                    4.3 使用Google Test库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="4.4-chinese.html">
            
                <a href="4.4-chinese.html">
            
                    
                    4.4 使用Boost Test进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="4.5-chinese.html">
            
                <a href="4.5-chinese.html">
            
                    
                    4.5 使用动态分析来检测内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="4.6-chinese.html">
            
                <a href="4.6-chinese.html">
            
                    
                    4.6 预期测试失败
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="4.7-chinese.html">
            
                <a href="4.7-chinese.html">
            
                    
                    4.7 使用超时测试运行时间过长的测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="4.8-chinese.html">
            
                <a href="4.8-chinese.html">
            
                    
                    4.8 并行测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="4.9-chinese.html">
            
                <a href="4.9-chinese.html">
            
                    
                    4.9 运行测试子集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="4.10-chinese.html">
            
                <a href="4.10-chinese.html">
            
                    
                    4.10 使用测试固件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 配置时和构建时的操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 使用平台无关的文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 配置时运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 构建时运行自定义命令:Ⅰ. 使用add_custom_command
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 构建时运行自定义命令:Ⅱ. 使用add_custom_target
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter5/5.5-chinese.html">
            
                <a href="../chapter5/5.5-chinese.html">
            
                    
                    5.5 构建时为特定目标运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../chapter5/5.6-chinese.html">
            
                <a href="../chapter5/5.6-chinese.html">
            
                    
                    5.6 探究编译和链接命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../chapter5/5.7-chinese.html">
            
                <a href="../chapter5/5.7-chinese.html">
            
                    
                    5.7 探究编译器标志命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../chapter5/5.8-chinese.html">
            
                <a href="../chapter5/5.8-chinese.html">
            
                    
                    5.8 探究可执行命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../chapter5/5.9-chinese.html">
            
                <a href="../chapter5/5.9-chinese.html">
            
                    
                    5.9 使用生成器表达式微调配置和编译
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 生成源码
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 使用Python在配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 构建时使用Python生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 记录项目版本信息以便报告
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter6/6.5-chinese.html">
            
                <a href="../chapter6/6.5-chinese.html">
            
                    
                    6.5 从文件中记录项目版本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../chapter6/6.6-chinese.html">
            
                <a href="../chapter6/6.6-chinese.html">
            
                    
                    6.6 配置时记录Git Hash值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.7" data-path="../chapter6/6.7-chinese.html">
            
                <a href="../chapter6/6.7-chinese.html">
            
                    
                    6.7 构建时记录Git Hash值
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 构建项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 使用函数和宏重用代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 将CMake源代码分成模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 编写函数来测试和设置编译器标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 用指定参数定义函数或宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter7/7.5-chinese.html">
            
                <a href="../chapter7/7.5-chinese.html">
            
                    
                    7.5 重新定义函数和宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter7/7.6-chinese.html">
            
                <a href="../chapter7/7.6-chinese.html">
            
                    
                    7.6 使用废弃函数、宏和变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="../chapter7/7.7-chinese.html">
            
                <a href="../chapter7/7.7-chinese.html">
            
                    
                    7.7 add_subdirectory的限定范围
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="../chapter7/7.8-chinese.html">
            
                <a href="../chapter7/7.8-chinese.html">
            
                    
                    7.8 使用target_sources避免全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="../chapter7/7.9-chinese.html">
            
                <a href="../chapter7/7.9-chinese.html">
            
                    
                    7.9 组织Fortran项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 超级构建模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 使用超级构建模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 使用超级构建管理依赖项:Ⅰ.Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 使用超级构建管理依赖项:Ⅱ.FFTW库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 使用超级构建管理依赖项:Ⅲ.Google Test框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 使用超级构建支持项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter9/9.0-chinese.html">
            
                <a href="../chapter9/9.0-chinese.html">
            
                    
                    第9章 语言混合项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../chapter9/9.1-chinese.html">
            
                <a href="../chapter9/9.1-chinese.html">
            
                    
                    9.1 使用C/C++库构建Fortran项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../chapter9/9.2-chinese.html">
            
                <a href="../chapter9/9.2-chinese.html">
            
                    
                    9.2 使用Fortran库构建C/C++项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../chapter9/9.3-chinese.html">
            
                <a href="../chapter9/9.3-chinese.html">
            
                    
                    9.3 使用Cython构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../chapter9/9.4-chinese.html">
            
                <a href="../chapter9/9.4-chinese.html">
            
                    
                    9.4 使用Boost.Python构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="../chapter9/9.5-chinese.html">
            
                <a href="../chapter9/9.5-chinese.html">
            
                    
                    9.5 使用pybind11构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.6" data-path="../chapter9/9.6-chinese.html">
            
                <a href="../chapter9/9.6-chinese.html">
            
                    
                    9.6 使用Python CFFI混合C，C++，Fortran和Python
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../chapter10/10.0-chinese.html">
            
                <a href="../chapter10/10.0-chinese.html">
            
                    
                    第10章 编写安装程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../chapter10/10.1-chinese.html">
            
                <a href="../chapter10/10.1-chinese.html">
            
                    
                    10.1 安装项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../chapter10/10.2-chinese.html">
            
                <a href="../chapter10/10.2-chinese.html">
            
                    
                    10.2 生成输出头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../chapter10/10.3-chinese.html">
            
                <a href="../chapter10/10.3-chinese.html">
            
                    
                    10.3 输出目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../chapter10/10.4-chinese.html">
            
                <a href="../chapter10/10.4-chinese.html">
            
                    
                    10.4 安装超级构建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 打包项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 生成源代码和二进制包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 通过PyPI发布使用CMake/pybind11构建的C++/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 通过PyPI发布使用CMake/CFFI构建C/Fortran/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter11/11.4-chinese.html">
            
                <a href="../chapter11/11.4-chinese.html">
            
                    
                    11.4 以Conda包的形式发布一个简单的项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.5" data-path="../chapter11/11.5-chinese.html">
            
                <a href="../chapter11/11.5-chinese.html">
            
                    
                    11.5 将Conda包作为依赖项发布给项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter12/12.0-chinese.html">
            
                <a href="../chapter12/12.0-chinese.html">
            
                    
                    第12章 构建文档
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter12/12.1-chinese.html">
            
                <a href="../chapter12/12.1-chinese.html">
            
                    
                    12.1 使用Doxygen构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter12/12.2-chinese.html">
            
                <a href="../chapter12/12.2-chinese.html">
            
                    
                    12.2 使用Sphinx构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter12/12.3-chinese.html">
            
                <a href="../chapter12/12.3-chinese.html">
            
                    
                    12.3 结合Doxygen和Sphinx
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../chapter13/13.0-chinese.html">
            
                <a href="../chapter13/13.0-chinese.html">
            
                    
                    第13章 选择生成器和交叉编译
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../chapter13/13.1-chinese.html">
            
                <a href="../chapter13/13.1-chinese.html">
            
                    
                    13.1 使用CMake构建Visual Studio 2017项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../chapter13/13.2-chinese.html">
            
                <a href="../chapter13/13.2-chinese.html">
            
                    
                    13.2 交叉编译hello world示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../chapter13/13.3-chinese.html">
            
                <a href="../chapter13/13.3-chinese.html">
            
                    
                    13.3 使用OpenMP并行化交叉编译Windows二进制文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter14/14.0-chinese.html">
            
                <a href="../chapter14/14.0-chinese.html">
            
                    
                    第14章 测试面板
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter14/14.1-chinese.html">
            
                <a href="../chapter14/14.1-chinese.html">
            
                    
                    14.1 将测试部署到CDash
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter14/14.2-chinese.html">
            
                <a href="../chapter14/14.2-chinese.html">
            
                    
                    14.2 CDash显示测试覆盖率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter14/14.3-chinese.html">
            
                <a href="../chapter14/14.3-chinese.html">
            
                    
                    14.3 使用AddressSanifier向CDash报告内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter14/14.4-chinese.html">
            
                <a href="../chapter14/14.4-chinese.html">
            
                    
                    14.4 使用ThreadSaniiser向CDash报告数据争用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter15/15.0-chinese.html">
            
                <a href="../chapter15/15.0-chinese.html">
            
                    
                    第15章 使用CMake构建已有项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter15/15.1-chinese.html">
            
                <a href="../chapter15/15.1-chinese.html">
            
                    
                    15.1 如何开始迁移项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter15/15.2-chinese.html">
            
                <a href="../chapter15/15.2-chinese.html">
            
                    
                    15.2 生成文件并编写平台检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter15/15.3-chinese.html">
            
                <a href="../chapter15/15.3-chinese.html">
            
                    
                    15.3 检测所需的链接和依赖关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.4" data-path="../chapter15/15.4-chinese.html">
            
                <a href="../chapter15/15.4-chinese.html">
            
                    
                    15.4 复制编译标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.5" data-path="../chapter15/15.5-chinese.html">
            
                <a href="../chapter15/15.5-chinese.html">
            
                    
                    15.5 移植测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.6" data-path="../chapter15/15.6-chinese.html">
            
                <a href="../chapter15/15.6-chinese.html">
            
                    
                    15.6 移植安装目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.7" data-path="../chapter15/15.7-chinese.html">
            
                <a href="../chapter15/15.7-chinese.html">
            
                    
                    15.7 进一步迁移的措施
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.8" data-path="../chapter15/15.8-chinese.html">
            
                <a href="../chapter15/15.8-chinese.html">
            
                    
                    15.8 项目转换为CMake的常见问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../chapter16/16.0-chinese.html">
            
                <a href="../chapter16/16.0-chinese.html">
            
                    
                    第16章 可能感兴趣的书
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../chapter16/16.1-chinese.html">
            
                <a href="../chapter16/16.1-chinese.html">
            
                    
                    16.1 留下评论——让其他读者知道你的想法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >4.1 创建一个简单的单元测试</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="41-&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;">4.1 &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;</h1>
<p><strong>NOTE</strong>:<em>&#x6B64;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5728; <a href="https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-04/recipe-01" target="_blank">https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-04/recipe-01</a> &#x4E2D;&#x627E;&#x5230;&#xFF0C;&#x5305;&#x542B;&#x4E00;&#x4E2A;C++&#x7684;&#x793A;&#x4F8B;&#x3002;&#x8BE5;&#x793A;&#x4F8B;&#x5728;CMake 3.5&#x7248;(&#x6216;&#x66F4;&#x9AD8;&#x7248;&#x672C;)&#x4E2D;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x5DF2;&#x7ECF;&#x5728;GNU/Linux&#x3001;macOS&#x548C;Windows&#x4E0A;&#x8FDB;&#x884C;&#x8FC7;&#x6D4B;&#x8BD5;&#x3002;</em></p>
<p>CTest&#x662F;CMake&#x7684;&#x6D4B;&#x8BD5;&#x5DE5;&#x5177;&#xFF0C;&#x672C;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F7F;&#x7528;CTest&#x8FDB;&#x884C;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x3002;&#x4E3A;&#x4E86;&#x4FDD;&#x6301;&#x5BF9;CMake/CTest&#x7684;&#x5173;&#x6CE8;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x4F1A;&#x5C3D;&#x53EF;&#x80FD;&#x7684;&#x7B80;&#x5355;&#x3002;&#x8BA1;&#x5212;&#x662F;&#x7F16;&#x5199;&#x548C;&#x6D4B;&#x8BD5;&#x80FD;&#x591F;&#x5BF9;&#x6574;&#x6570;&#x6C42;&#x548C;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x53EA;&#x4F1A;&#x5BF9;&#x6574;&#x6570;&#x8FDB;&#x884C;&#x7D2F;&#x52A0;&#xFF0C;&#x4E0D;&#x5904;&#x7406;&#x6D6E;&#x70B9;&#x6570;&#x3002;&#x5C31;&#x50CF;&#x5E74;&#x8F7B;&#x7684;&#x5361;&#x5C14;&#x2022;&#x5F17;&#x91CC;&#x5FB7;&#x91CC;&#x5E0C;&#x2022;&#x9AD8;&#x65AF;(Carl Friedrich Gauss)&#xFF0C;&#x88AB;&#x4ED6;&#x7684;&#x8001;&#x5E08;&#x6D4B;&#x8BD5;&#x4ECE;1&#x5230;100&#x6C42;&#x548C;&#x6240;&#x6709;&#x81EA;&#x7136;&#x6570;&#x4E00;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x8981;&#x6C42;&#x4EE3;&#x7801;&#x505A;&#x540C;&#x6837;&#x7684;&#x4E8B;&#x60C5;&#x3002;&#x4E3A;&#x4E86;&#x8BF4;&#x660E;CMake&#x6CA1;&#x6709;&#x5BF9;&#x5B9E;&#x9645;&#x6D4B;&#x8BD5;&#x7684;&#x8BED;&#x8A00;&#x8FDB;&#x884C;&#x4EFB;&#x4F55;&#x9650;&#x5236;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x4EC5;&#x4F7F;&#x7528;C++&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#xFF0C;&#x8FD8;&#x4F7F;&#x7528;Python&#x811A;&#x672C;&#x548C;shell&#x811A;&#x672C;&#x4F5C;&#x4E3A;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x3002;&#x4E3A;&#x4E86;&#x7B80;&#x5355;&#x8D77;&#x89C1;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4E0D;&#x4F7F;&#x7528;&#x4EFB;&#x4F55;&#x6D4B;&#x8BD5;&#x5E93;&#x6765;&#x5B9E;&#x73B0;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x5C06;&#x5728; &#x540E;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x4ECB;&#x7ECD;C++&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x3002;</p>
<h2 id="&#x51C6;&#x5907;&#x5DE5;&#x4F5C;">&#x51C6;&#x5907;&#x5DE5;&#x4F5C;</h2>
<p>&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#x7531;&#x4E09;&#x4E2A;&#x6587;&#x4EF6;&#x7EC4;&#x6210;&#x3002;&#x5B9E;&#x73B0;&#x6E90;&#x6587;&#x4EF6;<code>sum_integs.cpp</code>&#x5BF9;&#x6574;&#x6570;&#x5411;&#x91CF;&#x8FDB;&#x884C;&#x6C42;&#x548C;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x7D2F;&#x52A0;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;sum_integers.hpp&quot;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sum_integers</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; integers)</span> </span>{
    <span class="hljs-keyword">auto</span> sum = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i : integers) {
        sum += i;
    }
    <span class="hljs-keyword">return</span> sum;
}
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x662F;&#x5426;&#x662F;&#x4F18;&#x96C5;&#x7684;&#x5B9E;&#x73B0;&#x5E76;&#x4E0D;&#x91CD;&#x8981;&#xFF0C;&#x63A5;&#x53E3;&#x4EE5;<code>sum_integers</code>&#x7684;&#x5F62;&#x5F0F;&#x5BFC;&#x51FA;&#x3002;&#x63A5;&#x53E3;&#x5728;<code>sum_integers.hpp</code>&#x6587;&#x4EF6;&#x4E2D;&#x58F0;&#x660E;&#xFF0C;&#x8BE6;&#x60C5;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> once</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">sum_integers</span><span class="hljs-params">(<span class="hljs-keyword">const</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; integers)</span></span>;
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;main&#x51FD;&#x6570;&#x5728;<code>main.cpp</code>&#x4E2D;&#x5B9A;&#x4E49;&#xFF0C;&#x4ECE;<code>argv[]</code>&#x4E2D;&#x6536;&#x96C6;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#xFF0C;&#x5C06;&#x5B83;&#x4EEC;&#x8F6C;&#x6362;&#x6210;&#x6574;&#x6570;&#x5411;&#x91CF;&#xFF0C;&#x8C03;&#x7528;<code>sum_integers</code>&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x5C06;&#x7ED3;&#x679C;&#x6253;&#x5370;&#x5230;&#x8F93;&#x51FA;&#x4E2D;:</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;sum_integers.hpp&quot;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>

<span class="hljs-comment">// we assume all arguments are integers and we sum them up</span>
<span class="hljs-comment">// for simplicity we do not verify the type of arguments</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span> </span>{
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; integers;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span> i = <span class="hljs-number">1</span>; i &lt; argc; i++) {
        integers.push_back(<span class="hljs-built_in">std</span>::stoi(argv[i]));
    }
    <span class="hljs-keyword">auto</span> sum = sum_integers(integers);

    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; sum &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
}
</code></pre>
<p>&#x6D4B;&#x8BD5;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x4F7F;&#x7528;C++&#x5B9E;&#x73B0;(<code>test.cpp</code>)&#xFF0C;Bash shell&#x811A;&#x672C;&#x5B9E;&#x73B0;(<code>test.sh</code>)&#x548C;Python&#x811A;&#x672C;&#x5B9E;&#x73B0;(<code>test.py</code>)&#xFF0C;&#x53EA;&#x8981;&#x5B9E;&#x73B0;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x96F6;&#x6216;&#x975E;&#x96F6;&#x503C;&#xFF0C;&#x4ECE;&#x800C;CMake&#x53EF;&#x4EE5;&#x89E3;&#x91CA;&#x4E3A;&#x6210;&#x529F;&#x6216;&#x5931;&#x8D25;&#x3002;</p>
<p>C++&#x4F8B;&#x5B50;(<code>test.cpp</code>)&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x8C03;&#x7528;<code>sum_integers</code>&#x6765;&#x9A8C;&#x8BC1;1 + 2 + 3 + 4 + 5 = 15&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;sum_integers.hpp&quot;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">auto</span> integers = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>};

  <span class="hljs-keyword">if</span> (sum_integers(integers) == <span class="hljs-number">15</span>) {
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    }
}
</code></pre>
<p>Bash shell&#x811A;&#x672C;&#x8C03;&#x7528;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-shell">#!/usr/bin/env bash

EXECUTABLE=$1

OUTPUT=$($EXECUTABLE 1 2 3 4)

if [ &quot;$OUTPUT&quot; = &quot;10&quot; ]
then
    exit 0
else
    exit 1
fi
</code></pre>
<p>&#x6B64;&#x5916;&#xFF0C;Python&#x811A;&#x672C;&#x8C03;&#x7528;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;(&#x4F7F;&#x7528;<code>--executable</code>&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#x4F20;&#x9012;)&#xFF0C;&#x5E76;&#x4F7F;&#x7528;<code>--short</code>&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#x6267;&#x884C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> subprocess
<span class="hljs-keyword">import</span> argparse

<span class="hljs-comment"># test script expects the executable as argument</span>
parser = argparse.ArgumentParser()
parser.add_argument(<span class="hljs-string">&apos;--executable&apos;</span>,
                                         help=<span class="hljs-string">&apos;full path to executable&apos;</span>)
parser.add_argument(<span class="hljs-string">&apos;--short&apos;</span>,
                                         default=<span class="hljs-keyword">False</span>,
                    action=<span class="hljs-string">&apos;store_true&apos;</span>,
                    help=<span class="hljs-string">&apos;run a shorter test&apos;</span>)
args = parser.parse_args()

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">execute_cpp_code</span><span class="hljs-params">(integers)</span>:</span>
    result = subprocess.check_output([args.executable] + integers)
    <span class="hljs-keyword">return</span> int(result)

<span class="hljs-keyword">if</span> args.short:
    <span class="hljs-comment"># we collect [1, 2, ..., 100] as a list of strings</span>
    result = execute_cpp_code([str(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">1</span>, <span class="hljs-number">101</span>)])
    <span class="hljs-keyword">assert</span> result == <span class="hljs-number">5050</span>, <span class="hljs-string">&apos;summing up to 100 failed&apos;</span>
<span class="hljs-keyword">else</span>:
    <span class="hljs-comment"># we collect [1, 2, ..., 1000] as a list of strings</span>
    result = execute_cpp_code([str(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">1</span>, <span class="hljs-number">1001</span>)])
    <span class="hljs-keyword">assert</span> result == <span class="hljs-number">500500</span>, <span class="hljs-string">&apos;summing up to 1000 failed&apos;</span>
</code></pre>
<h2 id="&#x5177;&#x4F53;&#x5B9E;&#x65BD;">&#x5177;&#x4F53;&#x5B9E;&#x65BD;</h2>
<p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x9010;&#x6B65;&#x63CF;&#x8FF0;&#x5982;&#x4F55;&#x4E3A;&#x9879;&#x76EE;&#x8BBE;&#x7F6E;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<ol>
<li><p>&#x5BF9;&#x4E8E;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;C++11&#x652F;&#x6301;&#xFF0C;&#x53EF;&#x7528;&#x7684;Python&#x89E3;&#x91CA;&#x5668;&#xFF0C;&#x4EE5;&#x53CA;Bash shell:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">cmake_minimum_required</span>(VERSION <span class="hljs-number">3.5</span> FATAL_ERROR)

<span class="hljs-keyword">project</span>(recipe-<span class="hljs-number">01</span> LANGUAGES CXX)

<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD <span class="hljs-number">11</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_EXTENSIONS <span class="hljs-keyword">OFF</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD_REQUIRED <span class="hljs-keyword">ON</span>)

<span class="hljs-keyword">find_package</span>(PythonInterp REQUIRED)
<span class="hljs-keyword">find_program</span>(BASH_EXECUTABLE NAMES bash REQUIRED)
</code></pre>
</li>
<li><p>&#x7136;&#x540E;&#xFF0C;&#x5B9A;&#x4E49;&#x5E93;&#x53CA;&#x4E3B;&#x8981;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x4EE5;&#x53CA;&#x6D4B;&#x8BD5;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># example library</span>
<span class="hljs-keyword">add_library</span>(sum_integers sum_integers.cpp)

<span class="hljs-comment"># main code</span>
<span class="hljs-keyword">add_executable</span>(sum_up main.cpp)
<span class="hljs-keyword">target_link_libraries</span>(sum_up sum_integers)

<span class="hljs-comment"># testing binary</span>
<span class="hljs-keyword">add_executable</span>(cpp_test test.cpp)
<span class="hljs-keyword">target_link_libraries</span>(cpp_test sum_integers)
</code></pre>
</li>
<li><p>&#x6700;&#x540E;&#xFF0C;&#x6253;&#x5F00;&#x6D4B;&#x8BD5;&#x529F;&#x80FD;&#x5E76;&#x5B9A;&#x4E49;&#x56DB;&#x4E2A;&#x6D4B;&#x8BD5;&#x3002;&#x6700;&#x540E;&#x4E24;&#x4E2A;&#x6D4B;&#x8BD5;&#xFF0C; &#x8C03;&#x7528;&#x76F8;&#x540C;&#x7684;Python&#x811A;&#x672C;&#xFF0C;&#x5148;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#xFF0C;&#x518D;&#x4F7F;&#x7528;<code>--short</code>&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">enable_testing</span>()

<span class="hljs-keyword">add_test</span>(
  NAME bash_test
  COMMAND <span class="hljs-variable">${BASH_EXECUTABLE}</span> <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/test.sh $&lt;TARGET_FILE:sum_up&gt;
  )

<span class="hljs-keyword">add_test</span>(
  NAME cpp_test
  COMMAND $&lt;TARGET_FILE:cpp_test&gt;
  )

<span class="hljs-keyword">add_test</span>(
  NAME python_test_long
  COMMAND <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/test.py --executable $&lt;TARGET_FILE:sum_up&gt;
  )

<span class="hljs-keyword">add_test</span>(
  NAME python_test_short
  COMMAND <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/test.py --short --executable $&lt;TARGET_FILE:sum_up&gt;
  )
</code></pre>
</li>
<li><p>&#x73B0;&#x5728;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x597D;&#x914D;&#x7F6E;&#x548C;&#x6784;&#x5EFA;&#x4EE3;&#x7801;&#x3002;&#x5148;&#x624B;&#x52A8;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">$ mkdir -p build
$ cd build
$ cmake ..
$ cmake --build .
$ ./sum_up 1 2 3 4 5

15
</code></pre>
</li>
<li><p>&#x7136;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;<code>ctest</code>&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x96C6;&#xFF1A;</p>
<pre><code class="lang-shell">$ ctest

Test project /home/user/cmake-recipes/chapter-04/recipe-01/cxx-example/build
Start 1: bash_test
1/4 Test #1: bash_test ........................ Passed 0.01 sec
Start 2: cpp_test
2/4 Test #2: cpp_test ......................... Passed 0.00 sec
Start 3: python_test_long
3/4 Test #3: python_test_long ................. Passed 0.06 sec
Start 4: python_test_short
4/4 Test #4: python_test_short ................ Passed 0.05 sec
100% tests passed, 0 tests failed out of 4
Total Test time (real) = 0.12 sec
</code></pre>
</li>
<li><p>&#x8FD8;&#x5E94;&#x8BE5;&#x5C1D;&#x8BD5;&#x4E2D;&#x65AD;&#x5B9E;&#x73B0;&#xFF0C;&#x4EE5;&#x9A8C;&#x8BC1;&#x6D4B;&#x8BD5;&#x96C6;&#x662F;&#x5426;&#x80FD;&#x6355;&#x6349;&#x5230;&#x66F4;&#x6539;&#x3002;</p>
</li>
</ol>
<h2 id="&#x5DE5;&#x4F5C;&#x539F;&#x7406;">&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h2>
<p>&#x8FD9;&#x91CC;&#x7684;&#x4E24;&#x4E2A;&#x5173;&#x952E;&#x547D;&#x4EE4;&#xFF1A;</p>
<ul>
<li><code>enable_testing()</code>&#xFF0C;&#x6D4B;&#x8BD5;&#x8FD9;&#x4E2A;&#x76EE;&#x5F55;&#x548C;&#x6240;&#x6709;&#x5B50;&#x6587;&#x4EF6;&#x5939;(&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x628A;&#x5B83;&#x653E;&#x5728;&#x4E3B;<code>CMakeLists.txt</code>)&#x3002;</li>
<li><code>add_test()</code>&#xFF0C;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6D4B;&#x8BD5;&#xFF0C;&#x5E76;&#x8BBE;&#x7F6E;&#x6D4B;&#x8BD5;&#x540D;&#x79F0;&#x548C;&#x8FD0;&#x884C;&#x547D;&#x4EE4;&#x3002;</li>
</ul>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME cpp_test
  COMMAND $&lt;TARGET_FILE:cpp_test&gt;
  )
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;&#x4E86;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;:<code>$&lt;TARGET_FILE:cpp_test&gt;</code>&#x3002;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x662F;&#x5728;&#x751F;&#x6210;<strong>&#x6784;&#x5EFA;&#x7CFB;&#x7EDF;&#x751F;&#x6210;&#x65F6;</strong>&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x7B2C;5&#x7AE0;&#x7B2C;9&#x8282;&#x4E2D;&#x8BE6;&#x7EC6;&#x5730;&#x63CF;&#x8FF0;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x6B64;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x58F0;&#x660E;<code>$&lt;TARGET_FILE:cpp_test&gt;</code>&#x53D8;&#x91CF;&#xFF0C;&#x5C06;&#x4F7F;&#x7528;<code>cpp_test</code>&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;&#x7684;&#x5B8C;&#x6574;&#x8DEF;&#x5F84;&#x8FDB;&#x884C;&#x66FF;&#x6362;&#x3002;</p>
<p>&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x6D4B;&#x8BD5;&#x65F6;&#x975E;&#x5E38;&#x65B9;&#x4FBF;&#xFF0C;&#x56E0;&#x4E3A;&#x4E0D;&#x5FC5;&#x663E;&#x5F0F;&#x5730;&#x5C06;&#x53EF;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x7684;&#x4F4D;&#x7F6E;&#x548C;&#x540D;&#x79F0;&#xFF0C;&#x53EF;&#x4EE5;&#x786C;&#x7F16;&#x7801;&#x5230;&#x6D4B;&#x8BD5;&#x4E2D;&#x3002;&#x4EE5;&#x4E00;&#x79CD;&#x53EF;&#x79FB;&#x690D;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x8FD9;&#x4E00;&#x70B9;&#x975E;&#x5E38;&#x9EBB;&#x70E6;&#xFF0C;&#x56E0;&#x4E3A;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x548C;&#x53EF;&#x6267;&#x884C;&#x540E;&#x7F00;(&#x4F8B;&#x5982;&#xFF0C;Windows&#x4E0A;&#x662F;<code>.exe</code>&#x540E;&#x7F00;)&#x7684;&#x4F4D;&#x7F6E;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x3001;&#x6784;&#x5EFA;&#x7C7B;&#x578B;&#x548C;&#x751F;&#x6210;&#x5668;&#x4E4B;&#x95F4;&#x53EF;&#x80FD;&#x6709;&#x6240;&#x4E0D;&#x540C;&#x3002;&#x4F7F;&#x7528;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x5FC5;&#x663E;&#x5F0F;&#x5730;&#x4E86;&#x89E3;&#x4F4D;&#x7F6E;&#x548C;&#x540D;&#x79F0;&#x3002;</p>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x5C06;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x8981;&#x8FD0;&#x884C;&#x7684;<code>test</code>&#x547D;&#x4EE4;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME python_test_short
  COMMAND <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/test.py --short --executable $&lt;TARGET_FILE:sum_up&gt;
  )
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x6309;&#x987A;&#x5E8F;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#xFF0C;&#x5E76;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x7F29;&#x77ED;&#x603B;&#x6D4B;&#x8BD5;&#x65F6;&#x95F4;&#x5E76;&#x884C;&#x6267;&#x884C;&#x6D4B;&#x8BD5;(&#x7B2C;8&#x8282;)&#xFF0C;&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x7684;&#x5B50;&#x96C6;(&#x7B2C;9&#x8282;)&#x3002;&#x8FD9;&#x91CC;&#xFF0C;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49;&#x6D4B;&#x8BD5;&#x547D;&#x4EE4;&#xFF0C;&#x53EF;&#x4EE5;&#x4EE5;&#x4EFB;&#x4F55;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x96C6;&#x3002;CTest&#x5173;&#x5FC3;&#x7684;&#x662F;&#xFF0C;&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x7684;&#x8FD4;&#x56DE;&#x7801;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x662F;&#x5426;&#x901A;&#x8FC7;&#x3002;CTest&#x9075;&#x5FAA;&#x7684;&#x6807;&#x51C6;&#x7EA6;&#x5B9A;&#x662F;&#xFF0C;&#x8FD4;&#x56DE;&#x96F6;&#x610F;&#x5473;&#x7740;&#x6210;&#x529F;&#xFF0C;&#x975E;&#x96F6;&#x8FD4;&#x56DE;&#x610F;&#x5473;&#x7740;&#x5931;&#x8D25;&#x3002;&#x53EF;&#x4EE5;&#x8FD4;&#x56DE;&#x96F6;&#x6216;&#x975E;&#x96F6;&#x7684;&#x811A;&#x672C;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x505A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x3002;</p>
<p>&#x65E2;&#x7136;&#x77E5;&#x9053;&#x4E86;&#x5982;&#x4F55;&#x5B9A;&#x4E49;&#x548C;&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#xFF0C;&#x90A3;&#x4E48;&#x4E86;&#x89E3;&#x5982;&#x4F55;&#x8BCA;&#x65AD;&#x6D4B;&#x8BD5;&#x5931;&#x8D25;&#x4E5F;&#x5F88;&#x91CD;&#x8981;&#x3002;&#x4E3A;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x5F15;&#x5165;&#x4E00;&#x4E2A;bug&#xFF0C;&#x8BA9;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x90FD;&#x5931;&#x8D25;:</p>
<pre><code class="lang-shell">Start 1: bash_test
1/4 Test #1: bash_test ........................***Failed 0.01 sec
    Start 2: cpp_test
2/4 Test #2: cpp_test .........................***Failed 0.00 sec
    Start 3: python_test_long
3/4 Test #3: python_test_long .................***Failed 0.06 sec
    Start 4: python_test_short
4/4 Test #4: python_test_short ................***Failed 0.06 sec

0% tests passed, 4 tests failed out of 4

Total Test time (real) = 0.13 sec

The following tests FAILED:
1 - bash_test (Failed)
2 - cpp_test (Failed)
3 - python_test_long (Failed)
4 - python_test_short (Failed)
Errors while running CTest
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x60F3;&#x4E86;&#x89E3;&#x66F4;&#x591A;&#xFF0C;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x6587;&#x4EF6;<code>test/Temporary/lasttestsfailure.log</code>&#x3002;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x5305;&#x542B;&#x6D4B;&#x8BD5;&#x547D;&#x4EE4;&#x7684;&#x5B8C;&#x6574;&#x8F93;&#x51FA;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x5206;&#x6790;&#x9636;&#x6BB5;&#xFF0C;&#x8981;&#x67E5;&#x770B;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5730;&#x65B9;&#x3002;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;CLI&#x5F00;&#x5173;&#xFF0C;&#x53EF;&#x4EE5;&#x4ECE;CTest&#x83B7;&#x5F97;&#x66F4;&#x8BE6;&#x7EC6;&#x7684;&#x6D4B;&#x8BD5;&#x8F93;&#x51FA;&#xFF1A;</p>
<ul>
<li><code>--output-on-failure</code>:&#x5C06;&#x6D4B;&#x8BD5;&#x7A0B;&#x5E8F;&#x751F;&#x6210;&#x7684;&#x4EFB;&#x4F55;&#x5185;&#x5BB9;&#x6253;&#x5370;&#x5230;&#x5C4F;&#x5E55;&#x4E0A;&#xFF0C;&#x4EE5;&#x514D;&#x6D4B;&#x8BD5;&#x5931;&#x8D25;&#x3002;</li>
<li><code>-v</code>:&#x5C06;&#x542F;&#x7528;&#x6D4B;&#x8BD5;&#x7684;&#x8BE6;&#x7EC6;&#x8F93;&#x51FA;&#x3002;</li>
<li><code>-vv</code>:&#x542F;&#x7528;&#x66F4;&#x8BE6;&#x7EC6;&#x7684;&#x8F93;&#x51FA;&#x3002;</li>
</ul>
<p>CTest&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x65B9;&#x5FEB;&#x6377;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x91CD;&#x65B0;&#x8FD0;&#x884C;&#x4EE5;&#x524D;&#x5931;&#x8D25;&#x7684;&#x6D4B;&#x8BD5;&#xFF1B;&#x8981;&#x4F7F;&#x7528;&#x7684;CLI&#x5F00;&#x5173;&#x662F;<code>--rerun-failed</code>&#xFF0C;&#x5728;&#x8C03;&#x8BD5;&#x671F;&#x95F4;&#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;</p>
<h2 id="&#x66F4;&#x591A;&#x4FE1;&#x606F;">&#x66F4;&#x591A;&#x4FE1;&#x606F;</h2>
<p>&#x8003;&#x8651;&#x4EE5;&#x4E0B;&#x5B9A;&#x4E49;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME python_test_long
  COMMAND <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/test.py --executable $&lt;TARGET_FILE:sum_up&gt;
  )
</code></pre>
<p>&#x524D;&#x9762;&#x7684;&#x5B9A;&#x4E49;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x663E;&#x5F0F;&#x6307;&#x5B9A;&#x811A;&#x672C;&#x8FD0;&#x884C;&#x7684;<code>WORKING_DIRECTORY</code>&#x91CD;&#x65B0;&#x8868;&#x8FBE;&#xFF0C;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME python_test_long
  COMMAND <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> test.py --executable $&lt;TARGET_FILE:sum_up&gt;
  WORKING_DIRECTORY <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>
  )
</code></pre>
<p>&#x6D4B;&#x8BD5;&#x540D;&#x79F0;&#x53EF;&#x4EE5;&#x5305;&#x542B;<code>/</code>&#x5B57;&#x7B26;&#xFF0C;&#x6309;&#x540D;&#x79F0;&#x7EC4;&#x7EC7;&#x76F8;&#x5173;&#x6D4B;&#x8BD5;&#x4E5F;&#x5F88;&#x6709;&#x7528;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME python/long
  COMMAND <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> test.py --executable $&lt;TARGET_FILE:sum_up&gt;
  WORKING_DIRECTORY <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>
  )
</code></pre>
<p>&#x6709;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E3A;&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3002;&#x8FD9;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>set_tests_properties</code>&#x5B9E;&#x73B0;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set_tests_properties</span>(python_test
  PROPERTIES
    ENVIRONMENT
      ACCOUNT_MODULE_PATH=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>
      ACCOUNT_HEADER_FILE=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/account/account.h
      ACCOUNT_LIBRARY_FILE=$&lt;TARGET_FILE:account&gt;
  )
</code></pre>
<p>&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x5E73;&#x53F0;&#x4E0A;&#x5E76;&#x4E0D;&#x603B;&#x53EF;&#x884C;&#xFF0C;CMake&#x63D0;&#x4F9B;&#x4E86;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7247;&#x6BB5;&#x4E0E;&#x4E0A;&#x9762;&#x7ED9;&#x51FA;&#x7684;&#x4EE3;&#x7801;&#x7247;&#x6BB5;&#x76F8;&#x540C;&#xFF0C;&#x5728;&#x6267;&#x884C;&#x5B9E;&#x9645;&#x7684;Python&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#x4E4B;&#x524D;&#xFF0C;&#x901A;&#x8FC7;<code>CMAKE_COMMAND</code>&#x8C03;&#x7528;CMake&#x6765;&#x9884;&#x5148;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME
      python_test
  COMMAND
    <span class="hljs-variable">${CMAKE_COMMAND}</span> -E env
    ACCOUNT_MODULE_PATH=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>
    ACCOUNT_HEADER_FILE=<span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/account/account.h
    ACCOUNT_LIBRARY_FILE=$&lt;TARGET_FILE:account&gt;
    <span class="hljs-variable">${PYTHON_EXECUTABLE}</span>
    <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/account/test.py
  )
</code></pre>
<p>&#x540C;&#x6837;&#xFF0C;&#x8981;&#x6CE8;&#x610F;&#x4F7F;&#x7528;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;<code>$&lt;TARGET_FILE:account&gt;</code>&#x6765;&#x4F20;&#x9012;&#x5E93;&#x6587;&#x4EF6;&#x7684;&#x4F4D;&#x7F6E;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4F7F;&#x7528;<code>ctest</code>&#x547D;&#x4EE4;&#x6267;&#x884C;&#x6D4B;&#x8BD5;&#xFF0C;CMake&#x8FD8;&#x5C06;&#x4E3A;&#x751F;&#x6210;&#x5668;&#x521B;&#x5EFA;&#x76EE;&#x6807;(Unix Makefile&#x751F;&#x6210;&#x5668;&#x4E3A;<code>make test</code>&#xFF0C;Ninja&#x5DE5;&#x5177;&#x4E3A;<code>ninja test</code>&#xFF0C;&#x6216;&#x8005;Visual Studio&#x4E3A;<code>RUN_TESTS</code>)&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x8FD8;&#x6709;&#x53E6;&#x4E00;&#x79CD;(&#x51E0;&#x4E4E;)&#x53EF;&#x79FB;&#x690D;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">$ cmake --build . --target test
</code></pre>
<p>&#x4E0D;&#x5E78;&#x7684;&#x662F;&#xFF0C;&#x5F53;&#x4F7F;&#x7528;Visual Studio&#x751F;&#x6210;&#x5668;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>RUN_TESTS</code>&#x6765;&#x4EE3;&#x66FF;:</p>
<pre><code class="lang-shell">$ cmake --build . --target RUN_TESTS
</code></pre>
<p><strong>NOTE</strong>:<em><code>ctest</code>&#x63D0;&#x4F9B;&#x4E86;&#x4E30;&#x5BCC;&#x7684;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#x3002;&#x5176;&#x4E2D;&#x4E00;&#x4E9B;&#x5185;&#x5BB9;&#x5C06;&#x5728;&#x4EE5;&#x540E;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x63A2;&#x8BA8;&#x3002;&#x8981;&#x83B7;&#x5F97;&#x5B8C;&#x6574;&#x7684;&#x5217;&#x8868;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;<code>ctest --help</code>&#x6765;&#x67E5;&#x770B;&#x3002;&#x547D;&#x4EE4;<code>cmake --help-manual ctest</code>&#x4F1A;&#x5C06;&#x5411;&#x5C4F;&#x5E55;&#x8F93;&#x51FA;&#x5B8C;&#x6574;&#x7684;ctest&#x624B;&#x518C;&#x3002;</em></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="4.0-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 第4章 创建和运行测试">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="4.2-chinese.html" class="navigation navigation-next " aria-label="Next page: 4.2 使用Catch2库进行单元测试">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"4.1 创建一个简单的单元测试","level":"1.7.1","depth":2,"next":{"title":"4.2 使用Catch2库进行单元测试","level":"1.7.2","depth":2,"path":"content/chapter4/4.2-chinese.md","ref":"content/chapter4/4.2-chinese.md","articles":[]},"previous":{"title":"第4章 创建和运行测试","level":"1.7","depth":1,"path":"content/chapter4/4.0-chinese.md","ref":"content/chapter4/4.0-chinese.md","articles":[{"title":"4.1 创建一个简单的单元测试","level":"1.7.1","depth":2,"path":"content/chapter4/4.1-chinese.md","ref":"content/chapter4/4.1-chinese.md","articles":[]},{"title":"4.2 使用Catch2库进行单元测试","level":"1.7.2","depth":2,"path":"content/chapter4/4.2-chinese.md","ref":"content/chapter4/4.2-chinese.md","articles":[]},{"title":"4.3 使用Google Test库进行单元测试","level":"1.7.3","depth":2,"path":"content/chapter4/4.3-chinese.md","ref":"content/chapter4/4.3-chinese.md","articles":[]},{"title":"4.4 使用Boost Test进行单元测试","level":"1.7.4","depth":2,"path":"content/chapter4/4.4-chinese.md","ref":"content/chapter4/4.4-chinese.md","articles":[]},{"title":"4.5 使用动态分析来检测内存缺陷","level":"1.7.5","depth":2,"path":"content/chapter4/4.5-chinese.md","ref":"content/chapter4/4.5-chinese.md","articles":[]},{"title":"4.6 预期测试失败","level":"1.7.6","depth":2,"path":"content/chapter4/4.6-chinese.md","ref":"content/chapter4/4.6-chinese.md","articles":[]},{"title":"4.7 使用超时测试运行时间过长的测试","level":"1.7.7","depth":2,"path":"content/chapter4/4.7-chinese.md","ref":"content/chapter4/4.7-chinese.md","articles":[]},{"title":"4.8 并行测试","level":"1.7.8","depth":2,"path":"content/chapter4/4.8-chinese.md","ref":"content/chapter4/4.8-chinese.md","articles":[]},{"title":"4.9 运行测试子集","level":"1.7.9","depth":2,"path":"content/chapter4/4.9-chinese.md","ref":"content/chapter4/4.9-chinese.md","articles":[]},{"title":"4.10 使用测试固件","level":"1.7.10","depth":2,"path":"content/chapter4/4.10-chinese.md","ref":"content/chapter4/4.10-chinese.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","-sharing","github","pageview-count"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"pageview-count":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":30,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":30,"left":30,"top":30,"bottom":50}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《CMake Cookbook》中文版","language":"zh-hans","gitbook":"*","description":"作为对《CMake Cookbook》英文版的中文翻译。","direction":"ltr"},"file":{"path":"content/chapter4/4.1-chinese.md","mtime":"2020-09-22T10:39:01.941Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-22T11:21:21.825Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

